﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;

namespace ImportDatabase
{
    class Launcher
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine("Please provide the path to a properties file");
                Console.Read();
                Environment.Exit(1);
            }

            Console.WriteLine("Initializing importer");
            Importer importer = null;
            StreamReader propertiesStream = null;
            try
            {
                FileInfo properties = new FileInfo(args[0]);
                propertiesStream = File.OpenText(properties.FullName);
                FileInfo categorieFile = new FileInfo(propertiesStream.ReadLine());
                FileInfo uitgeverFile = new FileInfo(propertiesStream.ReadLine());
                FileInfo boekenFile = new FileInfo(propertiesStream.ReadLine());
                SqlConnection connection = new SqlConnection(propertiesStream.ReadLine());
                importer = new Importer(categorieFile, uitgeverFile, boekenFile, connection);
            }
            catch (ArgumentNullException ne)
            {
                Console.Error.WriteLine("Ongeldig properties bestand.");
                Console.Error.WriteLine("Het bestand moet vier regels bevatten:");
                Console.Error.WriteLine("het categorie bestand");
                Console.Error.WriteLine("het uitgever bestand");
                Console.Error.WriteLine("het boeken bestand");
                Console.Error.WriteLine("de connectie string");
                Console.Read();
                Environment.Exit(1);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Could not initialize importer");
                Console.Error.WriteLine(ex.Message);
                Console.Read();
                Environment.Exit(1);
            }
            finally
            {
                try
                {
                    propertiesStream.Close();
                }
                catch (Exception e)
                {
                }
            }

            Console.WriteLine("Importing files");
            try
            {
                importer.readAll();
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Could not import files");
                Console.Error.WriteLine(ex.Message);
                try
                {
                    importer.Close();
                }
                finally
                {
                    Console.Read();
                    Environment.Exit(1);
                }
            }

            Console.WriteLine("Done, closing connection");
            try
            {
                importer.Close();
            }
            catch (Exception ex)
            {
            }

            Console.WriteLine("Goodbye!");
            Console.Read();
        }
    }
}
